#!/usr/bin/python
# coding: utf-8


import os
import re
import sys
import random
from xml.dom import ext
from xml.dom import implementation
from xml.dom.minidom import parse
from mensaje import mensaje
from collocations import tTestGetCollocations, tTestReplaceCollocations




# Notar que están incorporados las posibilidades luego de encontrar collocations
candidatos = {
        u'cristina': [u'cfk',u'kristina',u'kirchner',u'klishtina',u'cristina_kirchner', u'cristina_fernandez', u'cristina_fernández', u'néstor_kirchner', u'nestor_kirchner', u'los_k'],
        u'lavagna':[u'sr lavagna', u'roberto_lavagna', u'dr_lavagna'],
        u'murphy':[u'bulldog', u'buldog', u'murfy', u'lópez_murphy', u'lopez_murphy', u'bull_dog'],
        u'sobisch': [u'bigoton', u'bigotón'],
        u'ripoll' :[u'vilma_ripoll'],
        u'solanas':[u'pino', u'pinosolanas', u'pino_solanas'],
        u'carrio': [u'carrio', u'gorda', u'la_gorda', u'sra_carrió', u'elisa_carrio', u'elisa_carrió'],
        u'pitrola' :[],
        u'saa': [u'adolfo', u'rodriguez_saa', u'rodríguez_saa', u'r_saa'],
        u'pitrola' :[]
}

# generate candidatosForms from previous dict of lists
candidatosForms = {}
for canonical in candidatos.iterkeys() :
        candidatosForms[canonical] = canonical
        for alternative in candidatos[canonical] : 
                candidatosForms[alternative] = canonical

#print "DEBUG:", candidatosForms

# Load all corpus from candidates
corpus_candidate = {}
for candidato in candidatos.iterkeys():
	print "DEBUG: Loading", candidato
	try:
		fd = open("../corpus/paginas_institucionales/"+candidato+".txt", "r")	# open file
	except:
		continue
	corpus = [] # corpus is a list of words
	# All corpus are in latin-1, load as chars, do not split into words
	corpus_candidate[candidato] = unicode(fd.read().decode('latin-1'))
	fd.close()


for candidate in candidatos.iterkeys():
	print "DEBUG: Writing", candidate
	m = mensaje()
	#Create a doctype using document type name, sysid and pubid
	dt = implementation.createDocumentType('noname', '', '')
	doc = implementation.createDocument("any", "any", dt)
	fd = open("gold_standard_"+candidate+".xml", "w")

	# XML header, in utf-8
	fd.write("""<?xml version='1.0' encoding='utf-8'?>
	<corpus>
	""")

	i = 0
	m_id = 0
	while (i<len(corpus_candidate[candidate])) :
		print "DEBUG:", candidate, "synthetic message", m_id
		length = random.randint(2048,4096)
        	m.autor = candidate
        	m.fecha = ""
        	m.titulo = ""
        	m.id = candidate+str(m_id)
		m.permalink = ""
		m.en_respuesta_a = ""
        	m.texto = corpus_candidate[candidate][i:i+length]
        	#print "DEBUG:", m.texto
		d = m.get_dom(doc)
		ext.PrettyPrint(d,fd,encoding='utf-8') # Finally everything to utf-8

		i += length
		m_id += 1
		
	fd.write("</corpus>")
	fd.close()

